package com.suzhe.ymall.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.suzhe.ymall.system.common.pojo.SysConstant;
import com.suzhe.ymall.system.pojo.SysResource;
import com.suzhe.ymall.system.mapper.SysResourceMapper;
import com.suzhe.ymall.system.service.ISysResourceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 资源管理 服务实现类
 * </p>
 *
 * @author suzhe
 * @since 2019-06-07
 */
@Service
public class SysResourceServiceImpl extends ServiceImpl<SysResourceMapper, SysResource> implements ISysResourceService {

    @Override
    public Set<String> getMenuPerms(Integer userId) {
        if (SysConstant.SUPER_ADMIN == userId){//如果是超级管理员
            List<SysResource> resources = list(new QueryWrapper<SysResource>().eq("type", SysConstant.ResourceType.MENU.getValue()));
            Set<String> menuPerms = new HashSet<>();
            resources.forEach(item -> {
                menuPerms.add(item.getPermission());
            });
            return menuPerms;
        }
        List<String> userPerms = baseMapper.queryUserPerms(userId, SysConstant.ResourceType.MENU.getValue());
        return new HashSet<>(userPerms);
    }

    @Override
    public Set<String> getActionPerms(Integer userId) {
        if (SysConstant.SUPER_ADMIN == userId){//如果是超级管理员
            List<SysResource> resources = list(new QueryWrapper<SysResource>().eq("type", SysConstant.ResourceType.ACTION.getValue()));
            Set<String> menuPerms = new HashSet<>();
            resources.forEach(item -> {
                menuPerms.add(item.getPermission());
            });
            return menuPerms;
        }
        List<String> userPerms = baseMapper.queryUserPerms(userId, SysConstant.ResourceType.ACTION.getValue());
        return new HashSet<>(userPerms);
    }

    @Override
    public List<SysResource> getAllResource() {


        return null;
    }


}
